/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.architexa.org.eclipse.gef.ui.views.palette;

import com.architexa.org.eclipse.draw2d.IFigure;
import com.architexa.org.eclipse.gef.EditPart;
import com.architexa.org.eclipse.gef.GraphicalEditPart;
import com.architexa.org.eclipse.gef.ui.palette.PaletteViewer;
import com.architexa.org.eclipse.gef.ui.palette.PaletteViewerProvider;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.util.Assert;
import org.eclipse.ui.part.Page;



/**
 * The default page for the PaletteView that works in conjunction with a 
 * PaletteViewerProvider.
 * 
 * @author Pratik Shah
 * @since 3.0
 */
public class PaletteViewerPage 
	extends Page
	implements PalettePage, IAdaptable {

/**
 * The PaletteViewerProvider that is used to create the PaletteViewer 
 */
protected PaletteViewerProvider provider;
/**
 * The PaletteViewer created for this page
 */
protected PaletteViewer viewer;

/**
 * Constructor
 * 
 * @param	pvProvider	the provider used to create the palette viewer
 */
public PaletteViewerPage(PaletteViewerProvider pvProvider) {
	Assert.isNotNull(pvProvider);
	provider = pvProvider;
}

/**
 * Creates the palette viewer and its control.
 * 
 * @see	Page#createControl(org.eclipse.swt.widgets.Composite)
 */
public void createControl(Composite parent) {
	viewer = provider.createPaletteViewer(parent);
}

/**
 * Releases the palette viewer from the edit domain
 * 
 * @see	Page#dispose()
 */
public void dispose() {
	if (provider.getEditDomain().getPaletteViewer() == viewer)
		provider.getEditDomain().setPaletteViewer(null);
	super.dispose();
	viewer = null;
}

/**
 * @see	IAdaptable#getAdapter(java.lang.Class)
 */
public Object getAdapter(Class adapter) {
	if (adapter == EditPart.class && viewer != null)
		return viewer.getEditPartRegistry().get(viewer.getPaletteRoot());
	if (adapter == IFigure.class && viewer != null) {
		Object obj = viewer.getEditPartRegistry().get(viewer.getPaletteRoot());
		if (obj instanceof GraphicalEditPart)
			return ((GraphicalEditPart)obj).getFigure();
	}
	return null;
}

/**
 * @return the palette viewer's control
 * @see Page#getControl()
 */
public Control getControl() {
	return viewer.getControl();
}

/**
 * Sets focus on the palette's control
 * 
 * @see Page#setFocus()
 */
public void setFocus() {
	getControl().setFocus();
}

}
